using System;
using Game.Scripts.Constant;
using Game.Scripts.ScriptableObject;
using Game.Scripts.Service;
using MoreMountains.Feedbacks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Zenject;

namespace Game.Scripts.Entity.UI.NextGameMenu
{
    public enum NextGameMenuOpenState
    {
        NextGame,
        Restart
    }

    public class NextGameMenu : GameMenuBase
    {
        [Header("Bindings")] [SerializeField] private Button restartButton;

        [SerializeField] private Button nextGameButton;

        [SerializeField] private MMF_Player openFeedback;

        private GameService _gameService;

        private IkunBus _ikunBus;

        [SerializeField] private TextMeshProUGUI allTimeBestScore;

        [SerializeField] private TextMeshProUGUI currentScore;
        private PlayerInputService _playerInputService;
        private AndroidCommunicationService _androidCommunicationService;

        [Inject]
        public void Construct(GameService gameService, IkunBus ikunBus, PlayerInputService playerInputService,
            AndroidCommunicationService androidCommunicationService)
        {
            _gameService = gameService;
            _ikunBus = ikunBus;
            _playerInputService = playerInputService;
            _androidCommunicationService = androidCommunicationService;
        }

        public void Open(NextGameMenuOpenState nextGameMenuOpenState)
        {
            if (_gameService.launchOption.HasFlag(LaunchOption.IfShowAds))
            {
                _androidCommunicationService.ShowAds();
            }
            _playerInputService.ifListenInput = false;
            if (nextGameMenuOpenState == NextGameMenuOpenState.Restart)
            {
                restartButton.gameObject.SetActive(true);
                nextGameButton.gameObject.SetActive(false);
            }
            else if (nextGameMenuOpenState == NextGameMenuOpenState.NextGame)
            {
                restartButton.gameObject.SetActive(false);
                nextGameButton.gameObject.SetActive(true);
            }

            openFeedback.PlayFeedbacks();
        }


        public override void Close()
        {
            base.Close();
            _playerInputService.ifListenInput = true;
        }


        public void ShowUI()
        {
            AssignUserScore();
            BackGroundBlur();
            _gameService.ResetGameState();
            base.Open();
        }


        private void AssignUserScore()
        {
            int currentPoints = _ikunBus.playerPoints.Value;
            currentScore.text = currentPoints.ToString();
            int prevScore = PlayerPrefs.GetInt(StorageKey.AllTimeBestScore.ToString());
            if (prevScore == 0)
            {
                allTimeBestScore.text = "";
                PlayerPrefs.SetInt(StorageKey.AllTimeBestScore.ToString(), currentPoints);
            }
            else if (prevScore < currentPoints)
            {
                allTimeBestScore.text = currentScore.text;
                PlayerPrefs.SetInt(StorageKey.AllTimeBestScore.ToString(), currentPoints);
            }
            else
            {
                allTimeBestScore.text = prevScore.ToString();
            }
        }
    }
}